const router = require('koa-router')()
// 操作数据库
const result = require('../../config/result')
// 图片上传
const {
    upload,
    confun,
    shopfun
} = require("../../cos/index")
// after 后台服务器
router.prefix('/api/after')



//商家图片上传
router.post('/upload', upload.single('file'), async ctx => {
    try {
        const res = await confun(ctx.file.filename, ctx.file.path, ctx.file.size)
        console.log("上传图片成功的路径", res)
        new result(ctx, 'SUCCESS', 200, 'https://' + res).answer()
    } catch (e) {
        new result(ctx, '上传图片失败!服务器发生错误', 500).answer()
    }
})
// 商品图片上传
router.post('/sopload', upload.single('file'), async ctx => {
    try {
        const res = await shopfun(ctx.file.filename, ctx.file.path, ctx.file.size)
        console.log("上传图片成功的路径", res)
        new result(ctx, 'SUCCESS', 200, 'https://' + res).answer()
    } catch (e) {
        new result(ctx, '上传图片失败!服务器发生错误', 500).answer()
    }
})

module.exports = router